﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Web.Security;
using HotelG11.Models;

namespace HotelG11.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View("Login");
        }

        [HttpPost]
        public ActionResult Login(string username, string password, string returnUrl)
        {
            G11KhachSanEntities G11 = new G11KhachSanEntities();
            List<G11_LoaiNhanVien> acc = new List<G11_LoaiNhanVien>();
            int? ma = G11.sp_KiemTraNV(username, password).FirstOrDefault();
            
            if( ma > 0)
                {
                    var res = G11.sp_DangNhapNV(username, password);
                    acc = res.ToList<G11_LoaiNhanVien>();
                    Session["LoaiTK"] = acc[0].maLoaiNV;
                    Session["TenLoaiTK"] = acc[0].tenLoaiNV;
                    FormsAuthentication.SetAuthCookie(username, true); 
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        string role = G11.sp_LogOnNV(username).FirstOrDefault();
                        if (role == "LOAINV1")
                        {
                            return RedirectToAction("Index", "NVQuanLy");
                        }
                        else
                        {
                            if (role == "LOAINV2")
                            {
                                return RedirectToAction("Index", "NVQuanTri");
                            }
                            else
                            {
                                if (role == "LOAINV3")
                                {
                                    return RedirectToAction("Index", "NVTiepTan");
                                }
                            }
                        }
                    }
                }
                else
                {
                    return RedirectToAction("Index", "Account");
                }

            return View("Login", returnUrl);
        }
        [HttpGet]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Account");
        }

        [HttpGet]
        public ActionResult KiemTraTaiKhoan(string tenTK, string maTK)
        {
            G11KhachSanEntities G11 = new G11KhachSanEntities();
            var ma = G11.sp_KiemTraNV(tenTK,maTK).FirstOrDefault();
            return Content(ma.ToString());
        }
    }
}
